package com.coder.code.ui

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.coder.code.R
import com.coder.widget.guide.GuideView
import com.coder.widget.guide.addToDecorView
import com.coder.widget.guide.setGuideView

class GuideActivity : AppCompatActivity() {
    private var mGuideView: GuideView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)
    }

    fun onButtonLayoutClick(targetView: View) {
        val guideLayout = LayoutInflater.from(this).inflate(R.layout.layout_guide_layout, null) as ViewGroup
        val replaceView = guideLayout.findViewById<View>(R.id.replaceView)
        val maskColor = ContextCompat.getColor(targetView.context, R.color.bg_main_guide)
        guideLayout.setGuideView(replaceView, targetView, maskColor).addToDecorView(this)
    }

    fun onButtonClick(targetView: View) {
        val direction = when (targetView.id) {
            R.id.btn_top -> GuideView.Direction.TOP
            R.id.btn_left -> GuideView.Direction.LEFT
            R.id.btn_right -> GuideView.Direction.RIGHT
            else -> GuideView.Direction.BOTTOM
        }
        mGuideView = GuideView.Builder()
                .setTargetView(targetView)
                .setHintView(getGuideView(targetView.context))
                .setHintViewDirection(direction)
                .setHintViewLayoutParams(RelativeLayout.LayoutParams(-1, -1))
                .setMaskColor(ContextCompat.getColor(targetView.context, R.color.bg_main_guide))
                .build()
        mGuideView?.show()
    }

    private fun getGuideView(context: Context) =
            LayoutInflater.from(context).inflate(R.layout.layout_guide_view, null).apply {
                setOnClickListener {
                    Log.d("GuideView", "setOnClickListener$it")
                    mGuideView?.hide()
                }
                Log.d("GuideView", "getGuideView$this")
            }


}